import {request} from '../../utils/http'

Page({
  data: {
    id:'',
    order_no:'',
    status:'',
    price:'',
    type:1,
    isLoadPlugin:false,
    order:{
      version: null
    },
    smid:''
  },
  onLoad(query:any) {
    if (query.order_no) {

      this.setData({
        id:query.id,
        order_no:query.order_no,
        status:query.status,
        price:query.price
      },() =>{ 
        this.fetch(query.order_no)
      })
    }
  },
  goOrder(){
    my.navigateTo({ url: '../orderDetail/index?id=' + this.data.order_no + '&type=1'})

  },
  goPortal(){
    my.switchTab({
      url:'/pages/index/index'
    })
  },
  submit(){
    if (!this.data.isLoadPlugin) {
      my.showToast({ content: "插件加载失败" });
      return;
    }

    if (this.data.order.version == 4) {
      this.goZM4(this.data.order)
    } else {
      this.initData(this.data.order);
      this.setData({
        type:2
      })
      // 跳转至插件首页
      my.navigateTo({
        url: "dynamic-plugin://2021003126645865/index",
      });
    }

   
  },
  goZM4(data){
    const accountInfo = my.getAccountInfoSync();
    const targetUrl = encodeURIComponent('/pages/zhima-result/zhima-result');
    let url =`/pages/cycle-card-buy/index?spcTemplateId=${data.card_template_id}&outOrderNo=${data.out_order_no}&shopId=${data.zm_shop_id}&jumpType=merchant&appId=${ accountInfo.miniProgram.appId || ''}&targetUrl=${targetUrl}`
    if (data.card_property_id) {
      url = `${url}&propertyId=${data.card_property_id}`
    }
    console.log(url)
    // return
    my.navigateToMiniProgram({
      appId: '2021004147687320',
      path: url,
      success: () => {
        console.log('navigateToMiniProgram success');
      },
      fail: res => {
        my.alert({
          title: 'navigateToMiniProgram fail',
          content: JSON.stringify(res),
        });
      },
    });
  },
  initData(order) {
        // 引用插件实例
        const plugin = requirePlugin('dynamic-plugin://2021003126645865');
        const ext = my.getExtConfigSync()
        console.log(ext.pid)
        
        // 宿主向插件传参数，通过实例调用init方法传参
        console.log(this.data.product, 'product');
        let d = new Date();
        // 开通
        const options = {
          merchantPid: this.data.smid, // 必传 - 商户PID
          productNo: order.product_id, // 必传 - 产品编号
          
          subscriptionType: order.pay_date == 1 ? 'MONTH' : 'WEEK', // 非必填, 订购类型, 默认为月卡
          productName: order.product_title, //必传 - 产品名称
          deductionAmount: order.second_amount, // 必传 - 每次扣减固定金额
          periods: order.total_stage, // 必传 - 开通总周期数
          downPayment: order.first_amount, // 非必填 - 前N期金额
          downPaymentPeriods: order.first_stage, // 非必填 -前多少期价格不一样，不传默认为1
          downPaymentDate: order.stage_list[1].date,
          outSubscriptionNo: order.out_order_no
        }
    
        if (order.shop.ant_shopid) {
          options['shopId'] = order.shop.ant_shopid
        }
        console.log('options', options)
        plugin.init({
          openOptions: options
        });
  },
  fetch(id) {
    request("/order/" + id, "get").then((res) => {
      if (res.code == 200) {
        this.setData({
          order: res.data,
        });
        // request("/shop/smid", "get",{shop_id:res.data.shop.id}).then((res) => {
        //   if (res.code == 200) {
        //     this.setData({
        //       smid: res.data.smid,
        //     }, () => {
        //       this.loadPlugin();
        //     });
        //   }
        // });
       
      }
     
    });
  },
  loadPlugin() {
    console.log("loadPlugin");
    my.loadPlugin({
      plugin: "2021003126645865@*", // 指定要加载的插件id和版本号，为 * 时自动选择版本
      success: () => {
        // 初始化数据 - 宿主传参
        console.log("success");
        this.setData({
          isLoadPlugin: true,
        });
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },
});

